package com.goldze.mvvmhabit.app


import android.app.Application
import android.content.Context

import com.facebook.common.logging.FLogDefaultLoggingDelegate
import com.facebook.common.logging.FLogDefaultLoggingDelegate.sInstance
import com.goldze.mvvmhabit.BuildConfig
import com.goldze.mvvmhabit.R
import com.goldze.mvvmhabit.ui.login.LoginActivity
import com.kongpf.commonhelper.ToastHelper
import com.squareup.leakcanary.LeakCanary
import com.tencent.bugly.Bugly.init
import com.tencent.bugly.beta.Beta.init
import io.github.kongpf8848.rxhttp.FixHttpLoggingInterceptor
import io.github.kongpf8848.rxhttp.RxHttpConfig
import io.github.kongpf8848.rxhttp.sample.http.interceptor.MockInterceptor
import io.github.kongpf8848.rxhttp.sample.utils.LogUtils
import me.goldze.mvvmhabit.base.BaseApplication
import me.goldze.mvvmhabit.crash.CaocConfig
import me.goldze.mvvmhabit.utils.KLog
import java.util.concurrent.TimeUnit

import io.github.kongpf8848.rxhttp.sample.utils.LogUtils.init

/**
 * Created by goldze on 2017/7/16.
 */

class AppApplication : BaseApplication() {


    override fun onCreate() {
        super.onCreate()
        _context = this
        //是否开启打印日志
        KLog.init(BuildConfig.DEBUG)
        Thread.setDefaultUncaughtExceptionHandler { t, e ->
            e.printStackTrace()
            LogUtils.e("Crash", e.message)
        }
        LogUtils.init(this, BuildConfig.DEBUG)
        ToastHelper.init(this)

        /**
         * 对RxHttp进行配置，可选
         */
        RxHttpConfig.getInstance()
                /**
                 * 失败重试次数
                 */
                .maxRetries(3)
                /**
                 * 每次失败重试间隔时间
                 */
                .retryDelayMillis(200)
                /**
                 * 证书校验，单向校验，即客户端校验服务端证书，null则为不校验
                 */
                //.certificate(AssetUtils.openFile(applicationContext,"xxx.cer"))
                /**
                 * 设置OkHttpClient.Builder(),RxHttp支持自定义OkHttpClient.Builder()
                 */
                .getBuilder().apply {
                    connectTimeout(60, TimeUnit.SECONDS)
                    readTimeout(60, TimeUnit.SECONDS)
                    writeTimeout(60, TimeUnit.SECONDS)
                    /**
                     * DEBUG模式下,添加日志拦截器,建议使用RxHttp中的FixHttpLoggingInterceptor,使用OkHttp的HttpLoggingInterceptor在上传下载的时候会有问题
                     */
                    if (BuildConfig.DEBUG) {
                        addInterceptor(FixHttpLoggingInterceptor().apply {
                            level = FixHttpLoggingInterceptor.Level.BODY
                        })
                        addInterceptor(MockInterceptor())
                    }
                }

        //初始化全局异常崩溃
        initCrash()
        //内存泄漏检测
        if (!LeakCanary.isInAnalyzerProcess(this)) {
            LeakCanary.install(this)
        }
    }

    private fun initCrash() {
        CaocConfig.Builder.create()
                .backgroundMode(CaocConfig.BACKGROUND_MODE_SILENT) //背景模式,开启沉浸式
                .enabled(true) //是否启动全局异常捕获
                .showErrorDetails(true) //是否显示错误详细信息
                .showRestartButton(true) //是否显示重启按钮
                .trackActivities(true) //是否跟踪Activity
                .minTimeBetweenCrashesMs(2000) //崩溃的间隔时间(毫秒)
                .errorDrawable(R.mipmap.ic_launcher) //错误图标
                .restartActivity(LoginActivity::class.java) //重新启动后的activity
                //                .errorActivity(YourCustomErrorActivity.class) //崩溃后的错误activity
                //                .eventListener(new YourCustomEventListener()) //崩溃后的错误监听
                .apply()
    }

    /**
     * 获得当前app运行的Application
     */
    companion object {
        var _context: AppApplication? = null
        fun getContext(): Context {
            return _context!!
        }
    }






}